package com.yanqu.road.dao.impl.statistics;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.statistics.RoleStatistics;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleStatisticsDaoImpl extends TempDataObjectDao<RoleStatistics> {

    public RoleStatistics getRoleStatistics() {
        String sql = "select * from t_u_role_statistics order by hoursTimeStamp desc";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                RoleStatistics data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public List<RoleStatistics> getRoleStatisticsList(long start, long end) {
        String sql = "select * from t_u_role_statistics where hoursTimeStamp>=? and hoursTimeStamp<=? order by hoursTimeStamp asc";
        ResultSet rs = null;
        CallableStatement call = null;
        List<RoleStatistics> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, start);
            call.setLong(2, end);
            rs = call.executeQuery();
            while (rs.next()) {
                RoleStatistics data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    @Override
    protected boolean updateData(RoleStatistics data) {
        String sql = "update t_u_role_statistics set createRoleCount=?,registerCount=?,createRoleCount2=? where hoursTimeStamp=?";
        PreparedStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getCreateRoleCount());
            call.setLong(i++, data.getRegisterCount());
            call.setLong(i++, data.getCreateRoleCount2());
            call.setLong(i++, data.getHoursTimeStamp());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean addData(RoleStatistics data) {
        String sql = "insert into t_u_role_statistics(hoursTimeStamp,createRoleCount,registerCount,createRoleCount2) values (?,?,?,?)";
        PreparedStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getHoursTimeStamp());
            call.setLong(i++, data.getCreateRoleCount());
            call.setLong(i++, data.getRegisterCount());
            call.setLong(i++, data.getCreateRoleCount2());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected RoleStatistics parseData(ResultSet rs) throws SQLException {
        RoleStatistics data = new RoleStatistics();
        data.setHoursTimeStamp(rs.getLong("hoursTimeStamp"));
        data.setCreateRoleCount(rs.getLong("createRoleCount"));
        data.setCreateRoleCount2(rs.getLong("createRoleCount2"));
        data.setRegisterCount(rs.getLong("registerCount"));
        return data;
    }
}
